var playlist = '';

function addSong(trackid) {
	$.post(playlist + '/add/' + trackid, function(data) {
		$('#feedback').show();
		$('#searchResultText').hide();
		if(data.success) {
			$('#feedback').html('<p>Låt lagt til i spillelisten!</p>').fadeOut(5000);
			$('#trackid').val('');
		}
		else {
			$('#feedback').html('<p>Huffda, noe gikk galt. Prøv igjen!</p>').fadeOut(5000);
		}
	});
}

$(document).ready(function() {
	$("#dialog-modal").hide();
	
	$('#searchResultText').hide();
	$('#sidebarbox').hide();

	$('#playlistSelected').click(function() {
		
		playlist = $('#playlist').val();
		$.getJSON("createplaylist?id=" + playlist,function(data) {
			
		});
		
		setInterval("updateSongsAjax()",10000);
		$('#playlistSelector').remove();
		$('#sidebarbox').show();
	});
	
	$("#trackname").keyup(function(event){
	    if(event.keyCode == 13){
	        $("#tracksubmit").click();
	    }
	});

	$('#tracksubmit').click(function() {
		
		$("#dialog-modal").dialog({
			width: 150,
			height: 150
		});
		
	    $.getJSON('http://ws.spotify.com/search/1/track.json?q=' + $('#trackname').val(), function(data) {
	    	$("#dialog-modal").dialog("close");
	    	$('#searchResult').empty();
	    	$('#searchResultText').show();
	        $.each(data.tracks, function(index,track) {
	            $('#searchResult').append("<li class='trackSearchResult' id='add-" + track.href + "'>" + track.artists[0].name + " - " + track.name + "</li>");
	            
	            // Mouseover effect for hovering result list
	        	$('.trackSearchResult').mouseover(function(data) {
	        		$(this).css('cursor','pointer');
	        		$(this).css('background-color','#7E7E7E');
	        		$(this).css('color','white');
	        	});
	        	
	        	$('.trackSearchResult').mouseleave(function(data) {
	        		$(this).css('cursor','default');
	        		$(this).css('background-color','white');
	        		$(this).css('color','#5C5B5B');
	        	});
	            
	        });
	        
            $('.trackSearchResult').click(function() {
    	    	var song_to_add = $(this).attr('id');
    	    	song_to_add = song_to_add.replace("add-","");
    	    	addSong(song_to_add);
    	    	$('#searchResult').empty();
    	    });
	        
	        $('li').addClass('ui-li ui-li-static ui-body-c');
	    });
	});
	
});
 
var uri_finished = '';

function appendHandlers() {
	
	// Click causes post-boost call to server side
	$('.song').click(function(data) {
		$.post(playlist + "/boost/" + $(this).attr('id'), function(data) {
			if(data.alreadyvoted == "true") {
				$('#feedback').show();
				$('#feedback').html('<p id="fadeout">Du har allerede stemt på denne låten').fadeOut(5000);
			}
			else {
				updateSongs(data.songs);
			}
		});	
	});
	
	// Mouseover effect for hovering songs
	$('.song').mouseover(function(data) {
		$(this).css('cursor','pointer');
		$(this).css('background-color','#7E7E7E');
		$(this).css('color','white');
	});
	
	$('.song').mouseleave(function(data) {
		$(this).css('cursor','default');
		$(this).css('background-color','white');
		$(this).css('color','#5C5B5B');
	});
	
}

function updateSongs(songs) {
	$('.songs').empty();
	$.each(songs, function(index,song) {
//		var uri_replaced = song.uri.replace(/:/g,"\\:");
		
//		if($('#' +  uri_replaced).length ) {
//			$('#' +  uri_replaced).html('<span id="left">' + song.artist + ' - ' + song.song + ': ' + song.score + '</span>');
//		}
//		else {
		$('.songs').append('<li id="' + song.uri + '" class="song"><span id="left">' + song.artist + ' - ' + song.song + ': ' + song.score + '</span></li>');
		$('li').addClass('ui-li ui-li-static ui-body-c');
		$('.song').off('click');
		appendHandlers();
//		}
	})
}

function updateSongsAjax() {
	$.get(playlist + '/get_current_songs', function(data) {
		updateSongs(data.songs);
	});
}